package edu.csumb.itcd.teammario;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

public class Tower{

    protected Point towerPosition;
    protected int towerWidth, towerHeight;
    protected Weapon weapon = new Weapon(0, 0);
    protected int lineOfSight;
    protected boolean isActive = false;
    protected Enemy target = null;
    protected boolean upgradeMenu = false;
    protected Rectangle towerRectangle;
    protected int towerCost = 1;
    private static int damage = 5;
    private static int rateOfFire = 15;
    
    public Tower(Point towerLocation, Weapon newWeapon)
    {
        weapon = newWeapon;
        towerPosition = towerLocation;
        towerWidth = 20;
        towerHeight = 20;
        isActive = true;
        lineOfSight = 500;
        towerCost = 1;
        towerRectangle = new Rectangle(towerPosition.x, towerPosition.y,towerWidth,towerHeight);

    }
    
    public Rectangle getTowerRectangle()
    {
    	towerRectangle = new Rectangle(towerPosition.x, towerPosition.y,towerWidth,towerHeight);
    	return towerRectangle; 
    }
    public int getLineOfSight() {
        return lineOfSight;
    }

    public void setLineOfSight(int lineOfSight) {
        this.lineOfSight = lineOfSight;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

    public void targeting(Enemy enemy)
    {

        Ellipse2D targetArea = new Ellipse2D.Double(towerPosition.getX() - ((towerWidth + lineOfSight)/2), towerPosition.getY() - ((towerHeight + lineOfSight)/2), towerWidth + lineOfSight, towerHeight + lineOfSight);
        if (target == null)
        {
            if (targetArea.intersects(enemy.getEnemyImage()) && enemy.isVisible())
            {
                target = enemy;
            }
        }
        
        if (target != null)
        {
            if (targetArea.intersects(target.getEnemyImage()) && target.isActive())
            {
                Point2D targetCenter = new Point2D.Double(target.getEnemyPosition().getX() + target.getWidth()/2, target.getEnemyPosition().getY() + target.getHeight()/2);
                weapon.towerFire(towerPosition, targetCenter);
            } else
            {
                target = null;
            }
        }
    }
    public void upgrade(boolean clickedON)
    {
    	upgradeMenu = clickedON;
    }
    public Point getTowerPosition() {
        return towerPosition;
    }

    public void setTowerPosition(Point towerPosition) {
        this.towerPosition = towerPosition;
    }

    public int getTowerWidth() {
        return towerWidth;
    }

    public void setTowerWidth(int towerWidth) {
        this.towerWidth = towerWidth;
    }

    public int getTowerHeight() {
        return towerHeight;
    }

    public void setTowerHeight(int towerHeight) {
        this.towerHeight = towerHeight;
    }
    
    public void paintTower(Graphics g)
    {
    	if (isActive == true)
        {
    		g.fillRect(towerPosition.x, towerPosition.y, towerWidth, towerHeight);
    		if (upgradeMenu == true)
    		{
    			Color upgradeMenuC = new Color(0, 0, 0, 100);
            	
    			g.setColor(upgradeMenuC);
    			g.fillRect(towerPosition.x - 5, towerPosition.y - 5, towerWidth + 10, towerHeight + 10);
    			g.drawString("Upgrade RateOfFire $5", towerPosition.x - 50, towerPosition.y - 3);
    		}
        }
    }
    
    public int getTowerCost()
    {
    	return towerCost;
    }

	public Weapon getWeapon() {
		return weapon;
	}

	public void setWeapon(Weapon weapon) {
		this.weapon = weapon;
	}
	
	public static void setDamage(int damage)
	{
		Tower.damage = damage;
	}
	
	public static int getDamage()
	{
		return damage;
	}
	
	public static void setRateOfFire(int rateOfFire)
	{
		Tower.rateOfFire = rateOfFire;
	}
	
	public static int getRateOfFire()
	{
		return rateOfFire;
	}
	public void updateTower()
	{
		weapon.setDamage(damage);
		weapon.setRateOfFire(rateOfFire);
	}
}